#ifndef __PID_H
#define __PID_H

#include "motor.h"

#define Kp 0.15
#define Ki 0.15
#define Kd 0.1

#define Kxp 2
#define Kxi 2
#define Kxd 2

#define Standard 500

#define NULL 0
#define LimitMax(input, max)   \
    {                          \
        if (input > max)       \
        {                      \
            input = max;       \
        }                      \
        else if (input < -max) \
        {                      \
            input = -max;      \
        }                      \
    }

		
		
enum PID_MODE
{
    PID_POSITION = 0,
    PID_enhance
};


typedef  struct{

	int mode;
	
	float kp;
	float ki;
	float kd;
	
	float max_out;
	float imax_out;
	
	float set;
	float now;
	
	float out;
	float pout;
	float iout;
	float dout;

  float dbuf[3];
	float error[3];


}pid_type_def;

extern float PID_calculate(pid_type_def*pid,float now,float set);
extern void PID_clear(pid_type_def *pid);
extern void PID_Init(pid_type_def*pid,int mode,const float PID[3],float max_out,float imax_out);
extern void stop(Motor_DataTypedef* Motor);
extern void pidsimple(Motor_DataTypedef* Motor, short int x0);
extern void first_pid(Motor_DataTypedef* Motor, short int x0);
extern int pid_pid(Motor_DataTypedef* Motor,short int ordered_meter,int now_pid,int changing_speed);
#endif